package com.qfedu.microuser.point.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.qfedu.common.core.entity.PointRule;
import com.qfedu.common.redis.utils.RedisUtil;
import com.qfedu.microuser.point.mapper.PointRuleMapper;
import com.qfedu.microuser.point.service.PointRuleService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.concurrent.TimeUnit;

@Service
public class PointRuleServiceImpl implements PointRuleService {
    @Resource
    private PointRuleMapper pointRuleMapper;
    @Resource
    private RedisUtil redisUtil;

    @Override
    public PointRule ruleInfoType(Integer pointType) {
        return pointRuleMapper.selectOne(Wrappers.lambdaQuery(PointRule.class)
                .eq(PointRule::getPointType, pointType));
    }

    @Override
    public List<PointRule> ruleList() {
        // 先查缓存，如果不存在，查数据库，将数据写入缓存
        List<PointRule> pointrule = (List<PointRule>) redisUtil.getCacheObject("pointrule");
        if (pointrule == null) {
            pointrule = pointRuleMapper.selectList(null);
            redisUtil.setCacheObject("pointrule", pointrule, 24 * 3600, TimeUnit.SECONDS);
        }
        return pointrule;
    }
}
